package com.daiyang.lotterysystem.controller;


import com.daiyang.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.daiyang.lotterysystem.common.errorexception.ControllerException;
import com.daiyang.lotterysystem.common.pojo.CommonReslut;
import com.daiyang.lotterysystem.common.utils.JacksonUtil;
import com.daiyang.lotterysystem.controller.param.UserRegisterParam;
import com.daiyang.lotterysystem.controller.result.UserRegisterResult;
import com.daiyang.lotterysystem.service.UserService;
import com.daiyang.lotterysystem.service.VerificationCodeService;
import com.daiyang.lotterysystem.service.dto.UserRegisterDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource(name="userServiceImpl")
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;

    @RequestMapping("/register")
    public CommonReslut<UserRegisterResult> register(@Validated @RequestBody UserRegisterParam param){
        //日志的打印
        logger.info("UserController register param:{}", JacksonUtil.writeValueAsString(param));
        //调用service层的register方法
        UserRegisterDTO    userRegisterDTO=userService.register(param);
        return CommonReslut.success(convertToUserRegisterResult(userRegisterDTO));

    }
    @RequestMapping("/verification-code/send")
    public CommonReslut<Boolean> sendVerificationCode(String phoneNumber) {
        logger.info("sendVerificationCode phoneNumber: " + phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonReslut.success(Boolean.TRUE);
    }


    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult result=new UserRegisterResult();
        if(userRegisterDTO==null) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
         result.setUserId(userRegisterDTO.getUserId());
        return result;
    }

}
